home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / StepPlatform Demo ƒ / sMovPlatform.c < prev    next >
Text File  |  1996-05-11  |  3KB  |  106 lines

  1. /* Platform sprite, moveable version, not faceless */
  2. /* */
  3.  
  4. #include "SAT.h"
  5. #include "myPlatform.h"
  6.  
  7. #ifndef abs
  8. #define abs(x)    (x>0?x:-x)
  9. #endif
  10.  
  11. #define    MovPlatformSpeed    2
  12.  
  13. /*************** In platformSAT.h *****************\
  14. *    #define    MaxV(me)        (*(MovPlatP)me->appPtr).MaxV
  15. *    #define    MinV(me)        (*(MovPlatP)me->appPtr).MinV
  16. *
  17. *    typedef struct {
  18. *            short    MaxV,MinV;
  19. *            short    MaxX,MinX;
  20. *            short    filled;    //unused
  21. *            } MovPlatRec,*MovPlatP;
  22. *********************************************/    
  23. FacePtr    platFace;
  24.  
  25. void InitMovPlatform()
  26. {
  27.     platFace = SATGetFace(139);
  28. }
  29.  
  30. pascal void SetupMovPlatform(SpritePtr me)
  31. {
  32.     Rect            r;
  33.     PolyHandle    pol;
  34.     
  35.     me->speed.v =-MovPlatformSpeed + SATRand(2) * 2;
  36.     me->face = platFace; 
  37.     me->appPtr =  NewPtr(sizeof(MovPlatRec));
  38.     if (me->appPtr)        {
  39.         (*(MovPlatP)me->appPtr).MaxV =me->position.v;
  40.         (*(MovPlatP)me->appPtr).MinV =me->kind;
  41.     }
  42.     SetRect(&me->hotRect, 0, 3, 60, 20);
  43.     me->task = &HandleMovPlatform;
  44.     me->hitTask = (void *)&HitMovPlatform;
  45. }
  46.  
  47. pascal void HandleMovPlatform(SpritePtr me)
  48. {
  49.     me->position.v +=  me->speed.v;
  50.     if(me->position.v < MinV(me))    me->speed.v =MovPlatformSpeed;
  51.     if(me->position.v > MaxV(me))    me->speed.v =-MovPlatformSpeed;
  52.     
  53.     if(me->speed.v == 0){
  54.         if    (me->position.v > (MaxV(me)-MinV(me)) / 2)
  55.             me->speed.v = -MovPlatformSpeed;
  56.         else
  57.             me->speed.v = MovPlatformSpeed;
  58.     }
  59.     
  60.     me->layer = -1*(me->position.v);
  61. }
  62.  
  63. pascal void HitMovPlatform(SpritePtr me, PlSpritePtr him)
  64. {
  65.     int    mini, i, min;
  66.     int    diff[5];
  67.         
  68.     if(him->task == (void *)HandlePlayerSprite) {
  69.         diff[1] =-me->hotRect2.top + (him->hotRect2.bottom);        /* (T)toB */
  70.         diff[2] =-him->hotRect2.top + (me->hotRect2.bottom);        /* (B)toT */
  71.         diff[3] =-me->hotRect2.left + (him->hotRect2.right);            /* LtoR */
  72.         diff[4] =-him->hotRect2.left + (me->hotRect2.right);            /* RtoL */
  73.         mini = 0;
  74.         min = 10000;
  75.         for(i =1; i <= 4; i++)
  76.             if(min > diff[i]){
  77.                 min = diff[i];
  78.                 mini = i;
  79.             }
  80.         switch(mini){
  81.             case    1: /*floor*/
  82.                     him->action = Stand;
  83.                     him->position.v = him->position.v - diff[1] + 2;  
  84.                     if(him->speed.v > 0)
  85.                         him->speed.v = 0;
  86.                     him->speed.h = 0;
  87.                     break;
  88.             case 2: /* ceiling */
  89.                     him->position.v = him->position.v + diff[2] + 1;
  90.                     if(him->speed.v < 0)
  91.                         him->speed.v = (him->speed.v)*(-1);
  92.                     break;
  93.             case 3: /*left*/
  94.                     him->position.h = him->position.h - diff[3] - 1;
  95.                     if(him->speed.h > 0)
  96.                         him->speed.h = (him->speed.h)*(-1);
  97.                     break;
  98.             case 4: /*right*/
  99.                     him->position.h =him->position.h + diff[4] + 1;
  100.                     if(him->speed.h < 0)
  101.                         him->speed.h = (him->speed.h)*(-1);
  102.                     break;
  103.         } /* switch */
  104.     }  /* if */
  105. }
  106.